﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GUIHexManager : MonoBehaviour {

	static public GUIHexManager current; //active player

	// Manages all the hex pieces in the GUI
	public GUIHex hexPrefab;
	public PlayerColor playerColor;
	public int playerScore;
	public int maxPlayerScore;
	public int maxHandSize; //pieces in hand
	
	private List<Transform> hexList;
	private List<Transform> hexHand;
	
	//left offset
	private GUIHex selectedHex = null;
	
	// Use this for initialization
	void Start () {
		//Debug.Log ("GUIHexManager.Start");
		gameObject.SetActive (false);
		hexPrefab.transform.position = new Vector3(1, 1, 0); //hide
		hexPrefab.enabled = false;
		hexList = new List<Transform> ();
		hexHand = new List<Transform> ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void setActivePlayer(bool activate) {
		enabled = activate;
		
		// local game: hide gui manager
		gameObject.SetActive (activate);
		foreach (Transform hex in hexList)
			hex.gameObject.SetActive (activate);

		if (activate)
			current = this;
	}

	public void clear() {
		foreach (Transform hex in hexList) {
			Destroy (hex.gameObject);
		}

		hexList.Clear();
		hexHand.Clear();
		playerScore = 0;
	}

	public void addScore( int score ) {
		playerScore += score;
		playerScore = Mathf.Min (playerScore, maxPlayerScore);
	}

	public GUIHex createHex() {
		GameObject hexObject = (GameObject)Instantiate (hexPrefab.gameObject);
		GUIHex guiHex = hexObject.GetComponent<GUIHex> ();
		guiHex.setActive(false);
		guiHex.enabled = true;
		
		/*Vector3 position = hexObject.transform.position;
		float posX = (guiHex.getWidth () / Screen.width) * hexList.Count;
		guiHex.transform.position = new Vector3 (posX, 0.0f, position.z);*/
		//Debug.Log ("GUIHexManager.createHex : hex.position=" + guiHex.transform.position);
		
		hexList.Add (hexObject.transform);
		
		return guiHex;
	}

	//called after a hex has been played, remove from team, hand, refill
	public void removeHex( GUIHex hex ) {
		//Debug.Log( "removeHex : " + hex );
		clearSelectedHex();
		
		hexHand.Remove (hex.transform);
		hexList.Remove (hex.transform);
		Destroy (hex.gameObject);

		RefillHand();
	}

	public int getHexCount() {
		return hexList.Count;
	}

	public void Shuffle() {
		//sort all pieces so that only the hand size is visible, deactivate the rest
		for (int i=0; i<hexList.Count; i++) {
			Transform hex = hexList[i];
			int rnd = Random.Range(i, hexList.Count);
			hexList[i] = hexList[rnd];
			hexList[rnd] = hex;
		}
		
		//and fill the current hand
		hexHand.Clear();
		RefillHand();
	}
	
	public void RefillHand() {
		//fill the current hand
		while (hexHand.Count < maxHandSize) {
			bool added = false;
			foreach (Transform hexTransform in hexList) {
				if (!hexHand.Contains(hexTransform)) {
					GUIHex hex = hexTransform.GetComponent<GUIHex>();
					hex.setActive(true);
					hexHand.Add(hexTransform);
					added = true;
					break;
				}
			}
			
			if (!added) break; //no more pieces to play
		}
		
		RefreshHand();
	}
	
	public void RefreshHand() {
		// reposition the pieces in the hand
		int hexIndex = 0;
		foreach (Transform hexTransform in hexHand) {
			GUIHex guiHex = hexTransform.GetComponent<GUIHex> ();
			float posX = (guiHex.getWidth () / Screen.width) * hexIndex;
			hexTransform.position = new Vector3(posX, 0.0f, 0.0f);
			hexIndex++;
		}
		
		hexIndex = 0;
		foreach (Transform hexTransform in hexList) {
			if (!hexHand.Contains(hexTransform)) {
				GUIHex guiHex = hexTransform.GetComponent<GUIHex> ();
				float posY = (guiHex.getHeight () / Screen.height) * 0.1f * hexIndex;
				hexTransform.position = new Vector3(0.9f, posY, 0.1f * hexIndex);
				hexIndex++;
			}
		}
	}

	public void clearSelectedHex() {
		if (selectedHex != null) {
			selectedHex.setSelected(false);
			selectedHex = null;
		}
	}

	public GUIHex getSelectedHex() {
		return selectedHex;
	}

	public bool CheckSelectHex() {
		bool result = false;
		Vector2 touchPos;
		#if UNITY_EDITOR || UNITY_STANDALONE_WIN
		if (Input.GetMouseButtonDown (0)) {
			touchPos = Input.mousePosition;
		#else
		if (Input.touchCount > 0 &&
			Input.GetTouch(0).phase == TouchPhase.Began) {
			touchPos = Input.GetTouch(0).position;
		#endif
			//Debug.Log("CheckSelectHex " + this.gameObject.name + " touchPos=" + touchPos);

			// determine which hex was selected
			//foreach (Transform hex in hexList) {
			foreach (Transform hex in hexHand) {
				GUIHex guiHex = hex.GetComponent<GUIHex>();
				if (guiHex.hitTest(touchPos)) {
					clearSelectedHex();
					guiHex.setSelected(true);
					selectedHex = guiHex;
					result = true;
					break;
				}
			}
		}
		return result;
	}
}
